<?php
namespace app\mobile\controller;

use app\model\Theme; 
use app\model\Worker; 

class WInput extends Base
{
    public function W_input()
    {
global $_G, $_P;
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('管理后台地址不正确');
}
$system=$_G['system'];
$theme = Theme::getThemeById($themeid);
$config = 	Theme::getConfig($themeid);
if(empty($theme['id'])){
	return_msg('管理后台地址不正确');
}
$myworker = Worker::getThemeWorkerByOpenidNew($_G['openid'],$themeid);
if(!($system['ruanjian']!=3 || $config['showshop']==0 || ($system['ruanjian']==3 && $config['showshop']==$myworker['company']))){
	$myworker = "";
}
$msg = Worker::gethoutaistats();
if (!empty($msg)) {
	return_msg($msg);
}

$op = !empty($_P['op']) ? $_P['op'] : 'display';

// if ($config['isxianxia']==0){
// 	return_msg('无权访问');
// }

if ($op == 'bangding') {

	$recordid = intval($_P['recordid']);
    $sql = "SELECT * FROM " . tablename (t_record) . "
								WHERE id = :id and (status > 0 or status=-2) ";
    $record = db_find($sql, array(":id"=>$recordid));

	if (!empty($record['openid'])){
		if($record['openid'] == $_G['openid']){
		    $recordid = $record['pid'];
		    $sharedata = array(
			    'themeid'=>$themeid,
			    'recordid'=> $recordid,
			);

		    if ($system['gongzhong'] =='1'){
		    	$domaintxt = $system['domainhead1'];
		    }else{
		    	$domaintxt = DOMAIN0;
		    }
			$inputurl = $_G['url_scheme'].$domaintxt."/".createMobileUrl('index',$sharedata);

			echo "<script language='javascript' type='text/javascript'>";
			echo "window.location.href='$inputurl';"; 
			echo "</script>";  
			exit;
		}else{
			return_msg("此定单已绑定微信:".$record['nickname']);
		}
	}
	if (empty($myworker)) {
	    $data = array(
	        'openid'   => $_G['openid'],
	        'uid'	   => $_G['member']['uid'],
	        'nickname' => $fans['nickname'],
	        'avatar'   => empty($fans['avatar']) ? $fans['headimgurl']:$fans['avatar'],
	        'pid'      => $recordid,
	    );

	    $r = db_update(t_record,$data,array("id"=>$record['id']));
        if(!empty($r)){
			$sharedata = array(
			    'themeid'=>$themeid,
			    'fopenid'=>$_G['openid'],
			    'fromuid'=>$_G['member']['uid'],
			    'workerid'=>$record['workerid'],
			);

		    if ($system['gongzhong'] =='1'){
		    	$domaintxt = $system['domainhead1'];
		    }else{
		    	$domaintxt = DOMAIN0;
		    }
			$inputurl = $_G['url_scheme'].$domaintxt."/".createMobileUrl('index',$sharedata);
			echo "<script language='javascript' type='text/javascript'>";
			echo "window.location.href='$inputurl';"; 
			echo "</script>";  
			exit;
        }else{
			return_msg("保存失败请重试");
        }
	}else{
		$sharedata = array(
		    'themeid'=>$themeid,
		    'op'=>'bangding',
		    'recordid'=> $recordid,
		);
		    if ($system['gongzhong'] =='1'){
		    	$domaintxt = $system['domainhead1'];
		    }else{
		    	$domaintxt = DOMAIN0;
		    }
			$inputurl = $_G['url_scheme'].$domaintxt."/".createMobileUrl('w_input',$sharedata);
	}
}else{
	if (empty($myworker)) {
		return_msg('您不是员工,无权访问');
	}
	$do = 'index';

	$feelist = array();

	if ($config['joinfee'] != '0.00' && !empty($config['joinfee'])){
		if ($system['jiage']==0) {
			$payname = '定金';
		}else{
			$payname = $config['joinname'];
		}
		array_push($feelist, array('name'=>$payname,'fee'=>$config['joinfee']));
	}
	if ($config['joinfee2'] != '0.00' && !empty($config['joinfee2'])){
		array_push($feelist, array('name'=>$config['joinname2'],'fee'=>$config['joinfee2']));
	}
	if ($config['joinfee3'] != '0.00' && !empty($config['joinfee3'])){
		array_push($feelist, array('name'=>$config['joinname3'],'fee'=>$config['joinfee3']));
	}
	if ($config['groupprice'] != '0.00' && !empty($config['groupprice'])){
		array_push($feelist, array('name'=>'拼团价','fee'=>$config['groupprice']));
	}
	if ($config['groupprice2'] != '0.00' && !empty($config['groupprice2'])){
		array_push($feelist, array('name'=>'拼团价②','fee'=>$config['groupprice2']));
	}
	if ($config['primeprice'] != '0.00' && !empty($config['primeprice'])){
		array_push($feelist, array('name'=>'单买价','fee'=>$config['primeprice']));
	}

}
if ($op == 'save') {
	$username = $_P['username'];
	$mobile = $_P['mobile'];
	$realfeetemp = explode("|", $_P['realfee']);
	$realfee = $realfeetemp[1];
    $payname = $realfeetemp[0];

	if (empty($username) || empty($mobile)) {
		return_msg('客户姓名与电话不可为空');
	}

	if (empty($realfee) || $realfee == "0.00"){
		if ($config['joinfee'] != "0.00"){
			$realfee=$config['joinfee'];
		}elseif ($config['groupprice'] != "0.00"){
			$realfee=$config['groupprice'];
		}else{
			$realfee=$config['primeprice'];
		}
	}

    if ($payname == "拼团价②" && $config['groupprice2'] != '0.00' && !empty($config['groupprice2']) && $realfee==$config['groupprice2']){
        $renshu = 1;
    }else{
        $renshu = 0;
    }
    if ($payname == "单买价" && $config['primeprice'] != '0.00' && !empty($config['primeprice']) && $realfee==$config['primeprice']){
        $paytype = 9;
    }else{
        $paytype = 0;
    }
	
	$uncode = $themeid.'-'.TIMESTAMP.rand(1000,1000000);
    $data = array(
        'uniacid'  => $_G['mpid'],
        'themeid'  => $themeid,
        'openid'   => '',
        'uid'	   => '',
        'nickname' => '',
        'avatar'   => '',
        'username' => $username,
        'mobile'   => $mobile,
        'addtime'  => TIMESTAMP,
        'fee'      => $realfee,
        'option' => $_P['shoptitle'],
        'address' => $_P['address_ordernew'],
        'datetime' => $_P['date_ordernew'],
        'code'     => '',
        'uncode'   => $uncode,
        'status'   => 3,
        'fromuid'  => $_G['member']['uid'],
        'fopenid'  => $_G['openid'],
        'workerid'  => $_G['member']['uid'],
        'pid'      => 0,
        'type'     => $paytype,
        'renshu'     => $renshu,
    );

    $addtime = TIMESTAMP-120;
        $sql = "SELECT * FROM " . tablename (t_record) . "
									WHERE themeid =:themeid and username = :username
											and mobile = :mobile and status=3 and addtime >" . $addtime . " ";
        $row = db_find($sql, array(":themeid"=>$themeid,":username"=>$username,":mobile"=>$mobile));

    if (!empty($row)) {
    	return_msg("客户“".$username."”在2分钟内录入过线下定单，若实际为多个定单请在2分钟以后再次录入");
    } else {
    	Theme::workerreport($themeid,$_G['member']['uid'],array('recordxx' =>1));
    	$r = $insertid = db_insert(t_record,$data);
    }
    

    if(!empty($r)){
	    $recordid = $insertid;
	    $sharedata = array(
		    'themeid'=>$themeid,
		    'op'=>'bangding',
		    'recordid'=> $recordid,
		);
		$inputurl = $_G['url_scheme'].$_SERVER['HTTP_HOST']."/".createMobileUrl('w_input',$sharedata);
			
		echo "<script language='javascript' type='text/javascript'>";
		echo "window.location.href='$inputurl';"; 
		echo "</script>";  
		exit;
	}else{
		return_msg("保存失败请重试");
	}
}

if($_G['companyname'] =='新飞驰软件'){
	$return_clicent = get_defined_vars();
return view('/fcworker/fc_input' , $return_clicent);
}else{
	$return_clicent = get_defined_vars();
return view('/yxworker/w_input' , $return_clicent);
}
}
}